var path;

$(document).ready(function(){
	path = $("#path").val();
	
	// find provinceid
	//$("#btn-select-region").live("click",findAllProvince);
	
	// find city by provinceid
	$(".filter-bd li").live("click",findCity);
	
	// hide choose region
	$("#mask").live("click",hideRegionBox);
});

function hideRegionBox(){
	$("#J_filterWrap").removeClass("show");
	$("#mask").remove();
}

function findCity(){
	var cur = $(this);
	// 样式
	cur.addClass("active").siblings("li").removeClass("active");
	
	//获取，当前li标签的flag的值
	var content = $.trim($(this).children().attr("flag"));
	
	switch(content){
	case "provice":
		var provice_id = $.trim($(this).children().attr("provice_id"));
		//获取城市信息
		getCityByProvinceId(provice_id);
		break;
	case "city":
		var city_id = $.trim($(this).children().attr("city_id"));
		//获取区域信息
		getAreaByCityId(city_id);
		break;
	case "area":
		var provice_ele = $("#stock_province_item li").filter(".active").children();
		var city_ele = $("#stock_city_item li").filter(".active").children();
		var country_ele = $("#stock_area_item li").filter(".active").children();
		//重置配送地址
		var totalAddress = provice_ele.html()+" "+city_ele.html()+" "+country_ele.html();
		//重置页面显示
		if(totalAddress.length>14){
			totalAddress = totalAddress.substring(0,12);
			$(".provi_city_area_id").html(totalAddress+"...");
		}else{
			$(".provi_city_area_id").html(totalAddress);
		}
		
		//更新库存信息
		var provinceId = provice_ele.attr("provice_id");
		var cityId = city_ele.attr("city_id");
		var areaId = country_ele.attr("area_id");
		loadArea(provinceId,cityId,areaId);
		 $("#J_filterWrap").removeClass("show");
		 $("#mask").remove();
		break;
	}
}

function findAllProvince(){
	$("body").append('<div id="mask" class="mask"></div>');
	$("html").addClass("lockscreen"),
    $("body").addClass("lockscreen"),
    
    $("#J_filterWrap").addClass("show");
	
	
	//隐藏省份,市区 ,显示country
	$("#stock_province_item").show();
	$("#stock_city_item").hide();
	$("#stock_area_item").hide();
	
	//异步加载省份信息,判断页面是否已加载过
	if($("#stock_province_item li").size()<=0){
		$("#stock_province_item ul").html("");
		getProvince();
	}
}

function getProvince(){
	$.ajax({
		type : "post",
		url :path+"/region/allProvince",
		async:true,
		dataType : "json",
		success: function(res){
		   if(res==null ||res =="") {return;}
		   for (var i = 0; i < res.length; i++) {
			   $("#stock_province_item ul").append('<li><a href="javascript:void(0);" provice_id="'+res[i].regionId+'" flag = "provice"  class="data_provice">'+res[i].regionName+'</a></li>');
		   }
		   
		   var recepientProvinceId = $("form[name='address-form']").find("input[name='recepientProvinceId']").val();
			if(!isEmpty(recepientProvinceId)){
				$("a[provice_id='"+recepientProvinceId+"']").parent().addClass("active");
			}
	   }
	}); 
}

function getCityByProvinceId(provinceId){
	//隐藏省份,显示市区
	$("#stock_province_item").hide();
	$("#stock_city_item").show();
	//清除页面缓存旧信息
	$("#stock_city_item ul").html("");
	$.ajax({
		type : "post",
		url :path+"/region/city/"+provinceId,
		dataType : "json",
		async:true,
		success: function(res){
		   if(res==null ||res =="") {return;}
		   
		   for (var i = 0; i < res.length; i++) {
			   $("#stock_city_item ul").append('<li><a href="javascript:void(0);" city_id="'+res[i].regionId+'" flag = "city" class="data_area">'+res[i].regionName+'</a></li>');
		   }
	   }
	}); 
}

function getAreaByCityId(cityId){
	//隐藏省份,市区 ,显示country
	$("#stock_province_item").hide();
	$("#stock_city_item").hide();
	$("#stock_area_item").show();
	//清除页面缓存旧信息
	$("#stock_area_item ul").html("");
	$.ajax({
		type : "post",
		url :path+"/region/area/"+cityId,
		dataType : "json",
		async:true,
		success: function(res){
		   if(res==null ||res =="") {return;}
		   for (var i = 0; i < res.length; i++) {
			   $("#stock_area_item ul").append('<li><a href="javascript:void(0);" area_id="'+res[i].regionId+'" flag = "area" class="select_area_js">'+res[i].regionName+'</a></li>');
		   }
	   }
	}); 
}

var loadArea = function(provinceId,cityId,areaId){

	$("input[name='recepientProvinceId']").val(provinceId);
	$("input[name='recepientCityId']").val(cityId);
	$("input[name='recepientAreaId']").val(areaId);
}